﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Boshii.Map.Bll.POI;
using Boshii.Map.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Data;
using System.Text;
using System.Web.Script.Services;

/// <summary>
///MyMapWebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。 
[System.Web.Script.Services.ScriptService]
public class MyMapWebService : System.Web.Services.WebService {

    public MyMapWebService () {

        //如果使用设计的组件，请取消注释以下行 
        //InitializeComponent(); 
    }

    //[WebMethod]
    //public bool AddMyTaobaoPOI(string poiName,

    [WebMethod]
    public string HelloWorld() {
        PoiBll poiBll = new PoiBll();
        PoiDs poiDs = new PoiDs();
        poiBll.GetAllMyPOI(poiDs);
        //DataTable resDt = new DataTable();
        //poiDs.T_POI.CopyToDataTable(resDt);
        //DataTable resDt = CopyDataTable(poiDs.T_POI);
        //JsonSerializerSettings jsSet = new JsonSerializerSettings();
        //string res = JsonConvert.SerializeObject(resDt, new DataTableConverter());
        string res = ToJson(poiDs.T_POI);
        return res;
    }

    #region dataTable转换成Json格式
    /// <summary>      
    /// dataTable转换成Json格式      
    /// </summary>      
    /// <param name="dt"></param>      
    /// <returns></returns>      
    public static string ToJson(DataTable dt)
    {
        StringBuilder jsonBuilder = new StringBuilder();
        jsonBuilder.Append("{\"");
        jsonBuilder.Append(dt.TableName.ToString());
        jsonBuilder.Append("\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            jsonBuilder.Append("{");
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                jsonBuilder.Append("\"");
                jsonBuilder.Append(dt.Columns[j].ColumnName);
                jsonBuilder.Append("\":\"");
                jsonBuilder.Append(dt.Rows[i][j].ToString());
                jsonBuilder.Append("\",");
            }
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("},");
        }
        jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
        jsonBuilder.Append("]");
        jsonBuilder.Append("}");
        return jsonBuilder.ToString();
    }

    #endregion dataTable转换成Json格式

    #region DataSet转换成Json格式
    /// <summary>      
    /// DataSet转换成Json格式      
    /// </summary>      
    /// <param name="ds">DataSet</param>      
    /// <returns></returns>      
    public static string ToJson(DataSet ds)
    {
        StringBuilder json = new StringBuilder();

        foreach (DataTable dt in ds.Tables)
        {
            json.Append("{\"");
            json.Append(dt.TableName);
            json.Append("\":");
            json.Append(ToJson(dt));
            json.Append("}");
        }
        return json.ToString();
    }
    #endregion 
    
}
